package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import model.ChessAraay;
import model.User;
import util.SocketUtil;
import util.StateCode;

/**
 * 
 * @author Administrator
 *
 */
public class ChessPanel extends JPanel implements Observer, ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private GridPanel gridPanel;
	private JLabel[] player = new JLabel[2];
	private String[] s1 = { "开始", "悔棋", "和棋", "认输", "回放" };
	private JButton[] button = new JButton[s1.length];
	private JLabel[] chessBox = new JLabel[2];
	private ChessAraay chessAraay;

	public ChessPanel() {

		setBackground(new Color(0xc0c0c0));
		setLayout(new BorderLayout());
		setOpaque(false);

		chessAraay = new ChessAraay();
		gridPanel = new GridPanel(chessAraay);
		add(gridPanel);
		chessAraay.addObserver(this);

		JPanel bottom = new JPanel();
		bottom.setOpaque(false);

		for (int i = 0; i < s1.length; i++) {
			button[i] = new JButton(s1[i]);
			bottom.add(button[i]);
			button[i].addActionListener(this);
		}
		add(bottom, BorderLayout.SOUTH);
		disableButton();

		JPanel top = new JPanel();
		top.setOpaque(false);
		JLabel title = new JLabel("网络五子棋");
		title.setFont(new Font("华文琥珀", Font.TRUETYPE_FONT, 24));
		title.setForeground(new Color(0x123456));
		JLabel author = new JLabel("             华信智原");
		author.setFont(new Font("华文行楷", Font.TRUETYPE_FONT, 14));
		author.setForeground(Color.RED);
		top.add(title);
		top.add(author);
		add(top, BorderLayout.NORTH);

		String[] s2 = { "玩家1", "玩家2" };
		JPanel[] lr = new JPanel[2];
		for (int i = 0; i < s2.length; i++) {
			lr[i] = new JPanel();
			lr[i].setOpaque(false);
			lr[i].setLayout(new GridBagLayout());
			GridBagConstraints gbc = new GridBagConstraints();
			JLabel image = new JLabel();
			image.setIcon(new ImageIcon(getClass().getResource("../image/photo" + i + ".png")));
			player[i] = new JLabel(s2[i]);
			gbc.gridy = 0;
			gbc.insets = new Insets(10, 20, 10, 20);
			lr[i].add(image, gbc);
			gbc.gridy = 1;
			lr[i].add(player[i], gbc);

			gbc.gridy = 3;
			chessBox[i] = new JLabel();
			chessBox[i].setPreferredSize(new Dimension(50, 37));
			lr[i].add(chessBox[i], gbc);
		}
		add(lr[0], BorderLayout.WEST);
		add(lr[1], BorderLayout.EAST);
	}

	@Override
	protected void paintComponent(Graphics g) {
		// TODO Auto-generated method stub
		super.paintComponent(g);
		Image img = new ImageIcon(getClass().getResource("../image/3.jpg")).getImage();
		g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
	}

	@Override
	public void update(Observable o, Object arg) {
		if (arg instanceof User) {
			User user = (User) arg;
			if (o.equals(user))
				player[1].setText(user.getName());
			else
				player[0].setText(user.getName());
		} else if (arg instanceof Integer) {
			int code = (int) arg;
			switch (code) {
			case StateCode.REQUESTSTART:
				int f = JOptionPane.showConfirmDialog(this, "对方请求开始，同意吗？", "提示信息", JOptionPane.YES_NO_OPTION);
				if (f == 0) {
					SocketUtil.sendMessage(StateCode.AGREESTART);
					enableButton();
					gridPanel.getPrompt().setMessage("");
					o.addObserver(gridPanel);
					chessAraay.setMy(false);
					setChessBox(false);
					chessAraay.clear();
				} else {
					SocketUtil.sendMessage(StateCode.DISAGREESTART);
				}
				break;
			case StateCode.AGREESTART:
				enableButton();
				gridPanel.getPrompt().setMessage("");
				o.addObserver(gridPanel);
				chessAraay.setMy(true);
				setChessBox(true);
				chessAraay.clear();
				break;
			case StateCode.DISAGREESTART:
				JOptionPane.showMessageDialog(this, "你被拒绝了！");
				gridPanel.getPrompt().setMessage("请点击开始");
				break;
			case StateCode.REQUESTREGRET:
				f = JOptionPane.showConfirmDialog(this, "对方请求悔棋，同意吗？", "提示信息", JOptionPane.YES_NO_OPTION);
				if (f == 0) {
					SocketUtil.sendMessage(StateCode.AGREEREGRET);
					chessAraay.regret();
				} else {
					SocketUtil.sendMessage(StateCode.DISAGREEREGRET);
				}
				break;
			case StateCode.AGREEREGRET:
				chessAraay.regret();
				break;
			case StateCode.DISAGREEREGRET:
				JOptionPane.showMessageDialog(this, "对方不同意");
				break;
			case StateCode.REPLAYOVER:
				button[s1.length - 1].setEnabled(true);
				break;
			case StateCode.CHANGECHESSBOX:
				if (chessAraay.isMy())
					setChessBox(false);
				else
					setChessBox(true);
				break;
			case StateCode.LINKRESET:
				chessAraay.clear();
				gridPanel.getPrompt().setPrompt("请点击开始", 48, Color.black, 1f);
				chessAraay.setMy(false);
				break;
			}
		} else if (arg instanceof Color) {
			Color color = (Color) arg;
			if (color.getRGB() == SocketUtil.getColor().getRGB()) {
				gridPanel.getPrompt().setPrompt("你赢了", 72, Color.green, 0.6f);
			} else {
				gridPanel.getPrompt().setPrompt("你输了", 72, Color.red, 0.6f);
			}
			disableButton();
		}
		repaint();
	}

	private void setChessBox(boolean myself) {
		int i = 1, j = 0;
		ImageIcon black = new ImageIcon(getClass().getResource("../image/blackChess.png"));
		ImageIcon white = new ImageIcon(getClass().getResource("../image/whiteChess.png"));
		ImageIcon ii;
		if (myself) {
			i = 0;
			j = 1;
		}
		chessBox[i].setIcon(null);
		if (SocketUtil.getColor().getRGB() == Color.BLACK.getRGB()) {
			ii = black;
			if (j == 0)
				ii = white;
			chessBox[j].setIcon(ii);
		} else {
			ii = white;
			if (j == 0)
				ii = black;
			chessBox[j].setIcon(ii);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		switch (command) {
		case "开始":
			SocketUtil.sendMessage(StateCode.REQUESTSTART);
			gridPanel.getPrompt().setPrompt("等待对方同意", 48, Color.black, 1f);
			gridPanel.repaint();
			break;
		case "悔棋":
			SocketUtil.sendMessage(StateCode.REQUESTREGRET);
			break;
		case "认输":
			break;
		case "和棋":
			break;
		case "回放":
			chessAraay.replay(gridPanel);
			button[s1.length - 1].setEnabled(false);
			break;
		}
	}

	private void disableButton() {
		button[0].setEnabled(true);
		button[1].setEnabled(false);
		button[2].setEnabled(false);
		button[3].setEnabled(false);
	}

	private void enableButton() {
		button[0].setEnabled(false);
		button[1].setEnabled(true);
		button[2].setEnabled(true);
		button[3].setEnabled(true);
	}
}
